﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Daybreak.Core.SceneGraph
{
    /// <summary>
    /// Walks through hierarhcy and calls Update(..) method for each node
    /// </summary>
    public class UpdateVisitor : NodeVisitor
    {
        Camera _camera;
        float _delta;

        public TimeSpanWatcher TimeStep;

        public UpdateVisitor() 
        {
            TimeStep = new TimeSpanWatcher(0);
        }

        public UpdateVisitor(float updatePerSecond)
        {
            TimeStep = new TimeSpanWatcher(updatePerSecond);
        }

        public void VisitScene(Scene.VisualScene scene, Camera camera, float elapsedTime)
        {
            _delta = TimeStep.GetNextDelta(elapsedTime);
            
            _camera = camera;

            if (_delta > 0)
            {
                camera.Update(_delta);
                scene.ApplyVisitor(this);
            }
        }


        public override void VisitGeode(Geode geode)
        {
            geode.Update(this, _delta);
        }

        public override void VisitGroup(Group group)
        {
            group.Update(this, _delta);

            // go through hierarchy
            base.VisitGroup(group);
        }

        public override void VisitTransform(Transform transform)
        {
            transform.Update(this, _delta);

            // go through hierarcy
            base.VisitTransform(transform);
        }

        public override void VisitSwitch(Switch node)
        {
            node.Update(this, _delta);
        }

        public override void VisitTerrain(TerrainNode terrain)
        {
            terrain.Update(this, _delta);
            terrain.UpdateTerrain(_camera);
        }
    }
}
